package com.hr.剑指offer.work1;

/**
 * @author 周恒哲
 * @date 2020/06/17
 **/
public class 面试题42_连续子数组的最大和 {
    public int maxSubArray(int[] nums) {
        int len = nums.length;
        if (len == 0) {
            return 0;
        }
        int[] dp = new int[len];
        dp[0] = nums[0];
        int max = dp[0];
        for (int i = 1; i < len; i++) {
            if (dp[i - 1] > 0) {
                dp[i] = dp[i - 1] + nums[i];
            } else {
                dp[i] = nums[i];
            }
            max = Math.max(max, dp[i]);

        }
        return max;
    }
}
